package com.frame.work.frame.deal.device0fbe;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;

import com.frame.work.common.constant.TypeEnum;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.DeviceAssociatedInformation;
import com.frame.work.entity.TblFingerprint;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.frame.deal.util.DeviceRelationMessageUtil;
import com.frame.work.frame.deal.util.WeakCurrentDeviceUtil;
import com.frame.work.mapper.SelectDeviceMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.frame.work.common.constant.CallType.TYPE_CONTROL;
import static com.frame.work.common.constant.CallType.TYPE_UP;
import static java.lang.Integer.parseInt;

/**
 * 指纹锁六fu
 */
@Data
@Slf4j
public class ZhiWenSuo_LiuFu_0FBE0203 extends BaseFrame {

    private String switchState;

    public ZhiWenSuo_LiuFu_0FBE0203(String gatewayMac, String data, String type) {

        //将基础数据进行处理
        super(gatewayMac, data);

        //根据不同类型进行判断
        if (TYPE_CONTROL.equals(type)) {
            try {
                ZhiWenSuo_LiuFu_0FBE0203_control(gatewayMac, data);
            } catch (Exception e) {
                log.error("error -> ZhiWenSuo_LiuFu_0FBE0203 -> type -> controlError:[{}]", e);
            }

        } else if (TYPE_UP.equals(type)) {
            try {
                ZhiWenSuo_LiuFu_0FBE0203_up(gatewayMac);
            } catch (Exception e) {
                log.error("error -> ZhiWenSuo_LiuFu_0FBE0203 -> type -> upError:[{}]", e);
            }

        }
    }

    public void ZhiWenSuo_LiuFu_0FBE0203_up(String gatewayMac) {
        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("reportType", this.getDataType());
        this.setActions(actions);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, JSONObject.toJSONString(this));

    }


    public void ZhiWenSuo_LiuFu_0FBE0203_control(String gatewayMac, String data) {

        //将基础数据进行处理
        String uart = StringUtils.substring(data, data.length() - 16, data.length());
        this.setDeviceType(getDateType(data));
        this.setProductType(getProductType(data));
        if ("80050200".equals(StringUtils.substring(uart, 0, 8))) {
            String type = TypeEnum.STATE.code();
            this.setSwitchState(getSwitchSate(uart));
            getAction(uart);
            //将数据发送到mqtt队列中
            JsonToMqtt.sendMQTT(gatewayMac, JSONObject.toJSONString(this));


            if ("01".equals(switchState)) {
                String content = "";
                String userToHex;
                try {
                    //用户类型Id
                    userToHex = Integer.toString(parseInt(getUserId(uart)));
                } catch (Exception e) {
                    //用户类型Id
                    userToHex = getUserId(uart);
                }


                SelectDeviceMapper selectDeviceMapper = SpringUtil.getBean(SelectDeviceMapper.class);
                List<DeviceAssociatedInformation> deviceUserMessage = selectDeviceMapper.getDevice1(this.getDeviceMac());
                if (CollectionUtils.isEmpty(deviceUserMessage)) {
                    //将数据发送到mqtt队列中
                    JsonToMqtt.sendMQTT(gatewayMac, JSONObject.toJSONString(this));
                    deviceUserMessage = DeviceRelationMessageUtil.getDeviceUserMessage(this.getDeviceMac());
                    WeakCurrentDeviceUtil.openTheDoorByKey(deviceUserMessage.get(0), userToHex, getName(getIdType(uart)), getIdType(uart));
                }

                switch (getIdType(uart)) {
                    //管理员指纹
                    case "01":
                        //普通指纹
                    case "02":
                        //客人指纹
                    case "03":
                        log.info("fingerprint -> number:[{}]", getIdType(uart));
                        TblFingerprint fingerprint = WeakCurrentDeviceUtil.openTheDoorByKey(deviceUserMessage.get(0), userToHex, getName(getIdType(uart)), getIdType(uart));
                        content = fingerprint.getName();
                        // 场景触发
                        WeakCurrentDeviceUtil.sceneTrigger(fingerprint);
                        break;
                    //挟持指纹
                    case "04":
                        log.info("六孚指纹锁挟持 -> fingerprint -> number:[{}]", getIdType(uart));
                        TblFingerprint fingerprint1 = WeakCurrentDeviceUtil.openTheDoorByKey(deviceUserMessage.get(0), userToHex, getName(getIdType(uart)), getIdType(uart));
                        content = fingerprint1.getName();
                        // 场景触发
                        WeakCurrentDeviceUtil.sceneTrigger(fingerprint1);
                        type = TypeEnum.WARNING.code();
                        break;
                    //遥控
                    case "05":
                        break;
                    //门铃
                    case "06":
                        break;
                    //普通密码
                    case "07":
                        content = "密码";
                        break;
                    //劫持密码
                    case "08":
                        content = "劫持密码";
                        type = TypeEnum.WARNING.code();
                        break;
                    //指纹加密码
                    case "09":
                        content = "指纹加密码";
                        break;
                    //网络开启(远程开门)
                    case "0A":
                        content = "远程";
                        break;
                    //门卡
                    case "0B":
                        content = "卡片";
                        break;
                    //指纹加卡
                    case "0C":
                        content = "指纹加卡";
                        break;
                    default:
                        break;
                }
                content = "被" + content + "打开";
                WeakCurrentDeviceUtil.SaveLogAndSendJpush(this.getDeviceMac(), this.getGatewayMac(), content, type);
            }
            // 电量上报
        } else if ("8003".equals(StringUtils.substring(data, data.length() - 12, data.length() - 8))) {
            //将actions信息放入json对象中
            Map<String, Object> actions1 = new HashMap(16);
            actions1.put("dianliang", getDianLiang(data));
            this.setActions(actions1);
            //处理好的json对象
            Object o1 = JSON.toJSON(this);
        }


    }


    /**
     * @Description:获取设备类型
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/2/4 18:11
     */
    private static String getDateType(String data) {
        return StringUtils.substring(data, 22, 24);
    }

    /**
     * @Description:获取产品类型
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/2/4 18:10
     */
    private static String getProductType(String data) {
        return StringUtils.substring(data, 24, 26);
    }

    private static String getUserId(String uart) {
        return StringUtils.substring(uart, 8, 10);
    }

    /**
     * @Description:获取钥匙类型
     * @param: uart
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/2/4 19:16
     */
    private static String getIdType(String uart) {
        return StringUtils.substring(uart, 10, 12);
    }

    /**
     * @Description:获取开关状态
     * @param: uart
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/2/4 19:18
     */
    private static String getSwitchSate(String uart) {
        return StringUtils.substring(uart, 12, 14);
    }

    /**
     * @Description:获取Action对象
     * @param: uart
     * @return: void
     * @author:wjming
     * @Date:2021/2/5 9:28
     */
    private void getAction(String uart) {

        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();

        actions.put("userId", getUserId(uart));
        actions.put("idType", getIdType(uart));
        actions.put("switchState", this.switchState);
        this.setActions(actions);
    }

    /**
     * @Description:指纹类型
     * @param: idType
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/2/5 10:19
     */
    private static String getName(String idType) {

        switch (idType) {
            //管理员指纹
            case "01":
                return "管理员";
            //普通指纹
            case "02":
                return "普通";
            //客人指纹
            case "03":
                return "客人";
            //劫持指纹
            case "04":
                return "挟持";
            default:
                return "";
        }
    }

    /**
     * @Description:获取电量信息
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/2/5 10:20
     */
    private static String getDianLiang(String data) {
        return StringUtils.substring(data, data.length() - 4, data.length() - 2);
    }


}
